package com.neusoft.lohas.common.utils;

import java.io.IOException;
import java.io.Writer;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonUtil {
	public static ObjectMapper objectMapper = new ObjectMapper();

	public static void write(Writer output, Object object) {
		try {
			objectMapper.writeValue(output, object);
		} catch (JsonGenerationException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static String WriteJsonObject(Object object) {
		if (object == null)
			return "";
		try {
			return objectMapper.writeValueAsString(object);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
			return "";
		}
	}

	public static <T> T readJsonObject(String object, Class<T> clazz) {
		if (object == null)
			return null;
		try {
			return objectMapper.readValue(object, clazz);
		} catch (JsonProcessingException e) {
			// e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

}
